package com.potato.architecture.ui;

import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.potato.architecture.R;
import com.potato.architecture.ui.adapter.TabAdapter;
import com.potato.base.base.BaseActivity;
import com.potato.base.base.BaseNullPresenter;
import com.potato.base.base.BasePresenter;
import com.potato.model1.Fragment.HomeFragment;
import com.potato.model2.ui.GifFragment;
import com.potato.model3.ui.MeFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements  BottomNavigationView.OnNavigationItemSelectedListener{

    private BottomNavigationView bottomNavigation;
    private ViewPager viewPager;
    private List<String> tabStr =new ArrayList<>();
    private List<Fragment> fragments =new ArrayList<>();
    private TabAdapter tabAdapter;

    @Override
    public BasePresenter loadPresenter() {
        return new BaseNullPresenter();
    }

    @Override
    public void initData() {
        tabStr.add("首页");
        tabStr.add("福利");
        tabStr.add("我的");
        fragments.add(new HomeFragment());
        fragments.add(new GifFragment());
        fragments.add(new MeFragment());
        tabAdapter=new TabAdapter(tabStr,fragments,getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(tabStr.size());
        viewPager.setAdapter(tabAdapter);
    }

    @Override
    public void initEvent() {
        super.initEvent();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
              MenuItem menuItem = bottomNavigation.getMenu().getItem(position);
              menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initView() {
        viewPager=findViewById(R.id.viewPager);
        bottomNavigation=findViewById(R.id.bottomNavigation);
        bottomNavigation.setOnNavigationItemSelectedListener(this::onNavigationItemSelected);
    }

    @Override
    public int layoutId() {
        return R.layout.activity_main;
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.tab_one:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tab_two:
                viewPager.setCurrentItem(1);
                break;
            case R.id.tab_three:
                viewPager.setCurrentItem(2);
                break;
        }
        return false;
    }

    @Override
    public void showError(String msg) {

    }
}
